import os
import sys
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg19 import preprocess_input
from keras.applications.vgg19 import decode_predictions
from keras.applications.vgg19 import VGG19
from keras.models import Model
import numpy as np
import json
import cv2
import time
from pymongo import MongoClient
import pymongo
img_path = 'E:\\image_pi_new_p2\\'

client = MongoClient(port=27017)
db=client['tiktok_p2']


#['thriftshop', 'upcycling', 'beautyhacks', 'wrestlemania', 'mystyle', 'superbowllv', 'defrosting', 'ratatouillemusical', 'productivity', 'loveyourinsecurities', 'falldiy', 'glasspainting', 'whereilive', 'foodie', 'nonuancenovember', 'roomtour', 'inverted', 'stemlife', 'fallfashion', 'festivefashion', 'science101', 'lovestory', 'vinylclub', 'homeproject', 'myroutine', 'nbaisback', 'cleantok', 'wishlist', 'imbusyrightnow', 'bakingszn',
# 'timewarpjump', 'healthyliving', 'plantparent', 'couplethings', 'watchmegrow', 'artmas', 'youwantmore', 'myrecommendation', 'nflplayoffs', 'tiktokdiy', 'blackandproud', 'sidetable', 'passover', 'fitnesslife', 'easydiy', 'stpatricksday', 'myhobby', 'beautyhack', 'holidayvibes', 'unwrapthedeals', 'timewarpwaterfall', 'meditation101', 'feliznavidad', 'onlinedating', 'dinnerparty', 'makeitvogue', 'smallbusiness', 'relationshipstorytime',
# 'familyimpression', 'dailyvlog', 'worldseries', 'cozyathome', 'animation', 'homebusiness', 'thankstoher', 'ramadanmubarak', 'tiktokfitness', 'coldweather', 'earthday', 'haventseen', 'wip', 'lifestyle', 'learnwithme', 'laughingduet', 'mobilegame', 'coversforlovers', 'gamenight', 'thrifting', 'olympicscountdown', 'imaghost', 'nativefamily', 'zodiacsign', 'passingthephone', 'easyrecipe', 'horrortok', 'hereforrmhc', 'bakedoats', 'puppybowl',
# 'kissyourpet', 'promszn', 'interiordesign', 'sfxmakeup', 'nbaallstar', 'blackcreatives', 'workingathome', 'skatelife', 'gaminglife', 'graphicdesign', 'tortillatrend', 'christmas2020', 'howbizarre', 'carhacks', 'snowstorm', 'housetour', 'mlkday', 'healthyheart', 'pleasedontgo', 'showupshowoff', 'happyhanukkah', 'perfectdrink', 'neontwin', '90saesthetic', 'holidaycountdown', 'tiktokfashionmonth', 'givingszn', 'royalrumble', 'recordsday',
# 'ootd', 'meleaving', 'homemade', 'happyhalloween', 'holidaydecor', 'welldone', 'rareaesthetic', 'carsoftiktok', 'foodtiktok', 'heinzhalloween', 'selfimprovement', 'songfacts', 'perfectgifts', 'joblife', 'autismawareness', 'melaninmagic', 'ecohacks', 'mifamilia', 'veteransday', 'thinkingabout', 'learnontiktok', 'seitan', 'growupwithme', 'ohno', 'tabletop', 'colorblast', 'yellow', 'diwali', 'emophase', 'problemsolved', 'neonshadow',
# 'cartiktok', 'homeimprovement', 'diyprojects', 'careeradvice', 'naturescereal', 'fitnessroutine', 'cleaningtiktok', 'hellowinter', 'bekind', 'gamingtiktok', 'winterbeautytips', 'familyrecipe', 'moneytok', 'tiktokwildlifeday', 'perfectmatch', 'myhaul', 'nbadraft', 'gamingsetup', 'visionboard', 'takeabite', 'wordsofwisdom', 'halloweenlook', '2018vs2021', 'fanedit', 'givethanksnotpranks', 'hyperfixated', 'inlove', 'yougotthis',
# 'inspirationalquotes', 'homecook', 'feelinggood', 'arabtiktok', 'happyholidays', 'vaccinatedfor', 'wildanimals', 'sheearnedit', 'rnbvibes', 'holidaytiktok', 'homecooked', 'womeninstem', 'dramaticmoments', 'nowlookatthis', 'givingthanks', 'lgbtqhistory', 'healthycooking', 'aprilfools', 'whenwomenwin', 'personalfinance', 'comingofage', 'roundofapplause', 'homeoffice', 'ourtype', 'comfortfood', 'notaperfectperson', 'homecafe', 'holidaycrafts',
# 'projectcar', 'clingypet', 'seashanty', 'baseballisback', 'holidaytreats', 'planttiktok', 'happyholi', 'foodontiktok', 'catchphrases', 'fetapasta', 'tiktokcars', 'transvisibilityday', '2021affirmations', 'goodmorning', 'greenscreenscan', 'stopasianhate', 'lunarnewyear', 'tiktokfood', 'welcome2021', 'valentinesdiy', 'springvibes', 'hellospring', 'onhold', 'lookbook', 'needtoknow', 'motivationmonday', 'typing', 'homediy', 'stepbystep',
# 'winterfit', 'happyeaster', 'yearontiktok', 'whatilearned', 'whenwewereyounger', 'weekendvibes', 'somethingyoulearned', 'groupchat', 'itwasntaphase', 'amongus', 'springoutfit', 'ontherunway', 'gamergoals', 'tiktoktailgate', 'athletesoftiktok', 'spookytreats', 'bye2020', 'mycostume', 'dayandnight', 'siblinggoals', 'nhlfaceoff', 'competitivegaming', 'galentinesday', 'albumcover', 'fallguysmoments', 'petsontiktok', 'holidaysourway', 'herstory',
# 'doctorsoftiktok', 'sabiasque', 'mypfp', 'crowdcheers', 'oscars', 'holidaymusic', 'makeblackhistory', 'fantheory', 'wintersports', 'tiktoktutorial', 'inkdrawing', 'diceroll', 'sehablaespañol', 'raisedby', 'nowruz', 'valentinesday', 'theatrekids', 'cocinando', 'readysetshop', 'skincare101', 'micellarrewind', 'sheesh', 'tipsandtricks', 'puppiesonly', 'halloweenishere', 'stylehacks', 'baseballtiktok', 'marchmadness', 'isthisavailable', 'colddays',
# 'weirdpets', 'gettheretogether', 'guitartok', 'winterfashion', 'thriftflip']

#collistt=['thriftshop', 'upcycling', 'beautyhacks', 'wrestlemania', 'mystyle', 'superbowllv', 'defrosting', 'ratatouillemusical', 'productivity', 'loveyourinsecurities', 'falldiy', 'glasspainting', 'whereilive', 'foodie', 'nonuancenovember', 'roomtour', 'inverted', 'stemlife', 'fallfashion', 'festivefashion', 'science101', 'lovestory', 'vinylclub', 'homeproject', 'myroutine', 'nbaisback', 'cleantok', 'wishlist', 'imbusyrightnow', 'bakingszn', 'timewarpjump', 'healthyliving', 'plantparent', 'couplethings', 'watchmegrow', 'artmas', 'youwantmore', 'myrecommendation', 'nflplayoffs', 'tiktokdiy', 'blackandproud', 'sidetable', 'passover', 'fitnesslife', 'easydiy', 'stpatricksday', 'myhobby', 'beautyhack', 'holidayvibes', 'unwrapthedeals', 'timewarpwaterfall', 'meditation101', 'feliznavidad', 'onlinedating', 'dinnerparty', 'makeitvogue', 'smallbusiness', 'relationshipstorytime', 'familyimpression', 'dailyvlog', 'worldseries', 'cozyathome', 'animation', 'homebusiness', 'thankstoher', 'ramadanmubarak', 'tiktokfitness', 'coldweather', 'earthday', 'haventseen', 'wip', 'lifestyle', 'learnwithme', 'laughingduet', 'mobilegame', 'coversforlovers', 'gamenight', 'thrifting', 'olympicscountdown', 'imaghost', 'nativefamily', 'zodiacsign', 'passingthephone', 'easyrecipe', 'horrortok', 'hereforrmhc', 'bakedoats', 'puppybowl', 'kissyourpet', 'promszn', 'interiordesign', 'sfxmakeup', 'nbaallstar', 'blackcreatives', 'workingathome', 'skatelife', 'gaminglife', 'graphicdesign', 'tortillatrend', 'christmas2020', 'howbizarre', 'carhacks', 'snowstorm', 'housetour', 'mlkday', 'healthyheart', 'pleasedontgo', 'showupshowoff', 'happyhanukkah', 'perfectdrink', 'neontwin', '90saesthetic', 'holidaycountdown', 'tiktokfashionmonth', 'givingszn', 'royalrumble', 'recordsday', 'ootd', 'meleaving', 'homemade', 'happyhalloween', 'holidaydecor', 'welldone', 'rareaesthetic', 'carsoftiktok', 'foodtiktok', 'heinzhalloween', 'selfimprovement', 'songfacts', 'perfectgifts', 'joblife', 'autismawareness', 'melaninmagic', 'ecohacks', 'mifamilia', 'veteransday', 'thinkingabout', 'learnontiktok', 'seitan', 'growupwithme', 'ohno', 'tabletop', 'colorblast', 'yellow', 'diwali', 'emophase', 'problemsolved', 'neonshadow', 'cartiktok', 'homeimprovement', 'diyprojects', 'careeradvice', 'naturescereal', 'fitnessroutine', 'cleaningtiktok', 'hellowinter', 'bekind', 'gamingtiktok', 'winterbeautytips', 'familyrecipe', 'moneytok', 'tiktokwildlifeday', 'perfectmatch', 'myhaul', 'nbadraft', 'gamingsetup', 'visionboard', 'takeabite', 'wordsofwisdom', 'halloweenlook', '2018vs2021', 'fanedit', 'givethanksnotpranks', 'hyperfixated', 'inlove', 'yougotthis', 'inspirationalquotes', 'homecook', 'feelinggood', 'arabtiktok', 'happyholidays', 'vaccinatedfor', 'wildanimals', 'sheearnedit', 'rnbvibes', 'holidaytiktok', 'homecooked', 'womeninstem', 'dramaticmoments', 'nowlookatthis', 'givingthanks', 'lgbtqhistory', 'healthycooking', 'aprilfools', 'whenwomenwin', 'personalfinance', 'comingofage', 'roundofapplause', 'homeoffice', 'ourtype', 'comfortfood', 'notaperfectperson', 'homecafe', 'holidaycrafts', 'projectcar', 'clingypet', 'seashanty', 'baseballisback', 'holidaytreats', 'planttiktok', 'happyholi', 'foodontiktok', 'catchphrases', 'fetapasta', 'tiktokcars', 'transvisibilityday', '2021affirmations', 'goodmorning', 'greenscreenscan', 'stopasianhate', 'lunarnewyear', 'tiktokfood', 'welcome2021', 'valentinesdiy', 'springvibes', 'hellospring', 'onhold', 'lookbook', 'needtoknow', 'motivationmonday', 'typing', 'homediy', 'stepbystep', 'winterfit', 'happyeaster', 'yearontiktok', 'whatilearned', 'whenwewereyounger', 'weekendvibes', 'somethingyoulearned', 'groupchat', 'itwasntaphase', 'amongus', 'springoutfit', 'ontherunway', 'gamergoals', 'tiktoktailgate', 'athletesoftiktok', 'spookytreats', 'bye2020', 'mycostume', 'dayandnight', 'siblinggoals', 'nhlfaceoff', 'competitivegaming', 'galentinesday', 'albumcover', 'fallguysmoments', 'petsontiktok', 'holidaysourway', 'herstory', 'doctorsoftiktok', 'sabiasque', 'mypfp', 'crowdcheers', 'oscars', 'holidaymusic', 'makeblackhistory', 'fantheory', 'wintersports', 'tiktoktutorial', 'inkdrawing', 'diceroll', 'sehablaespañol', 'raisedby', 'nowruz', 'valentinesday', 'theatrekids', 'cocinando', 'readysetshop', 'skincare101', 'micellarrewind', 'sheesh', 'tipsandtricks', 'puppiesonly', 'halloweenishere', 'stylehacks', 'baseballtiktok', 'marchmadness', 'isthisavailable', 'colddays', 'weirdpets', 'gettheretogether', 'guitartok', 'winterfashion', 'thriftflip']
collistt=['happyhalloween','nativefamily']
for hname in collistt:
    namepi=hname
    collist=[hname]
    videolist = {}
    i = 0
    for col in collist:
        videolist[col]=[]
        for item in db[col].find():
            videolist[col].append(item['_id'])
    for key in videolist.keys():
        print(key,len(videolist[key]))
    c=0
    os.system('mp4box -hint')
    for hashtag in videolist.keys():
        for vid in videolist[hashtag]:
            c+=1
            # if not os.path.exists('E:\\tiktok_new\\video\\' + hashtag + '\\' + vid + '.mp4'):
            #     continue
            vname=vid

            vidcap = cv2.VideoCapture('E:\\tiktok_new\\video\\' + hashtag + '\\' + vid + '.mp4')
            frame_count = vidcap.get(cv2.CAP_PROP_FRAME_COUNT)
            fps = vidcap.get(cv2.CAP_PROP_FPS)
            fl =frame_count/10/fps
            init=fl/2
            count = 0
            vidcap.set(cv2.CAP_PROP_POS_MSEC, (round((init+count * fl)*1000)))
            success, image = vidcap.read()

            while success:

                if os.path.exists(img_path + vname + "_%d.jpg" % count+'.jpg'):
                    count+=1
                    continue
                cv2.imwrite(img_path + vname + "_%d.jpg" % count, image[round(len(image)/10):round(len(image)-len(image)/10)])
                count = count + 1
                vidcap.set(cv2.CAP_PROP_POS_MSEC, round((init + count * fl) * 1000))
                success, image = vidcap.read()

                if count>9:
                    break
    idlist=[]
    for ht in videolist.keys():
        idlist.extend(videolist[ht])
    with open('E:\\data_pi\\imglist_temp'+namepi+'_new_p2.txt','w') as file_input:
        c=0
        for vname in os.listdir(img_path):
            v=vname.split('_')[0]
            if vname.endswith('.jpg') and v in idlist:
                c+=1
                file_input.write(img_path+vname+'\n')
        print(c)
    os.system('"'+'py -2 D:\\Work\\kusuri\\DeepSentiBank\\sentiBank.py '+'E:\\data_pi\\imglist_temp'+namepi+'_new_p2.txt'+'"')


    base_model = VGG19(weights='imagenet')
    model = Model(base_model.input, base_model.get_layer('fc2').output)
    col=''
    with open ('E:\\data_pi\\imglist_temp'+namepi+'_new_p2.txt','r',) as txt_input:
        with open ('E:\\data_pi\\imglist_temp'+namepi+'_new_p2.json','r',) as json_input:
            senti=json.load(json_input)
            c=0
            prev=''
            l=0
            avg=[]
            for line in txt_input:
                line=line.replace(img_path,'')
                print(line,c)
                iid=line.split('_')[0]
                imgnum=line.split('_')[1].replace('.jpg','').strip()
                for ht in videolist.keys():
                    if iid in videolist[ht]:
                        col=ht
                        break
                ori = db[col].find_one({'_id': iid})
                if len(ori['frame_level']['f'+imgnum]['img'])>0:
                    c+=1
                    continue
                print(iid)
                tempresult=[]
                line=line.strip()
                img = load_img(img_path+line,target_size=(224, 224))
                img = img_to_array(img)
                img = np.expand_dims(img, axis=0)
                img = preprocess_input(img)
                fc2_features = model.predict(img)
                prob_vgg = decode_predictions(base_model.predict(img), top=1000)
                for item in sorted(prob_vgg[0]):
                    tempresult.append(float(item[2]))
                for item in sorted(senti['images'][c]['bi-concepts']):
                    tempresult.append(float(senti['images'][c]['bi-concepts'][item]))
                for item in senti['images'][c]['features']:
                    tempresult.append(float(item))
                for ht in videolist.keys():
                    if iid in videolist[ht]:
                        col=ht
                        break
                db[col].update_one({'_id': iid}, {'$set':{'frame_level.f'+imgnum+'.img':tempresult}})
                print(db[col].find_one({'_id': iid}))
                c+=1
